package com.esun.api.fallback;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.esun.api.interfaces.IExampleApi;
import com.esun.api.request.ExampleRequestDto;
import com.esun.api.response.ExampleResponseDto;
import com.esun.common.enums.Message;

import com.esun.common.response.RestResponse;
import feign.hystrix.FallbackFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

/**
   * 样例表(Example)降级熔断处理
   *
   * @author John.xiao
   * @since 2021-08-17 14:36:47
   */
@Slf4j   
@Component
public class IExampleFallback implements FallbackFactory<IExampleApi> {
	@Override
	public IExampleApi create(Throwable throwable) {
		return new IExampleApi() {
			@Override
			public RestResponse getExampleByPage(int pageIndex, int pageSize) {
				ExampleRequestDto requestDto = new ExampleRequestDto();
				requestDto.setPageIndex(pageIndex);
				requestDto.setPageSize(pageSize);
				ExampleResponseDto responseDto = new ExampleResponseDto();
				BeanUtils.copyProperties(requestDto, responseDto);
				log.warn("\n==== [requestDto]: {}", JSONObject.toJSONString(requestDto, SerializerFeature.PrettyFormat));
				return new RestResponse(Message.QUERY_ERROR.getCode(), throwable.getMessage(),responseDto);
			}

			@Override
			public RestResponse addExample(List<ExampleRequestDto> requestDtoList) {
				log.info("\n==== [requestDtoList]: {}", JSONObject.toJSONString(requestDtoList, SerializerFeature.PrettyFormat));
				List<ExampleResponseDto> emptyExampleList = new ArrayList<>();
				return new RestResponse(Message.QUERY_ERROR.getCode(), throwable.getMessage(),emptyExampleList);
			}

			@Override
			public RestResponse updateExample(List<ExampleRequestDto> requestDtoList) {
				log.info("\n==== [requestDtoList]: {}", JSONObject.toJSONString(requestDtoList, SerializerFeature.PrettyFormat));
				List<ExampleResponseDto> emptyExampleList = new ArrayList<>();
				return new RestResponse(Message.QUERY_ERROR.getCode(), throwable.getMessage(),emptyExampleList);
			}

			@Override
			public RestResponse deleteExample(List<ExampleRequestDto> requestDtoList) {
				log.info("\n==== [requestDtoList]: {}", JSONObject.toJSONString(requestDtoList, SerializerFeature.PrettyFormat));
				List<ExampleResponseDto> emptyExampleList = new ArrayList<>();
				return new RestResponse(Message.QUERY_ERROR.getCode(), throwable.getMessage(),emptyExampleList);
			}

			@Override
			public RestResponse importExample(MultipartFile file) {
				log.info("\n==== [file]: {}", JSONObject.toJSONString(file, SerializerFeature.PrettyFormat));
				List<ExampleResponseDto> emptyExampleList = new ArrayList<>();
				return new RestResponse(Message.QUERY_ERROR.getCode(), throwable.getMessage(),emptyExampleList);
			}

			@Override
			public void exportExample(ExampleRequestDto requestDto) {
				log.info("\n==== [requestDto]: {}", JSONObject.toJSONString(requestDto, SerializerFeature.PrettyFormat));
			}

			@Override
			public void downloadTemplate(String path) {
				log.info("\n==== [path]: {}", JSONObject.toJSONString(path, SerializerFeature.PrettyFormat));
			}

		};
	}
}